#include "glare.d3d11.GpuBuffer.h"
#include "glare.d3d11.Driver.h"

namespace Glare {

	D3D11GpuBuffer::D3D11GpuBuffer(D3D11GpuResourceFactory* factory, const GpuBufferDescription& description)
		: GpuBuffer(description)
		, _factory(factory)
		, _d3d_buffer(NULL)
	{
		D3D11Driver* d3d11_rs = static_cast<D3D11Driver*>(factory->GetDriver());

		D3D11_BUFFER_DESC d3d11_buf_desc;

		// resource options
		d3d11_buf_desc.Usage               = (D3D11_USAGE)description.UsageFlags;
		d3d11_buf_desc.BindFlags           = description.BindFlags;
		d3d11_buf_desc.CPUAccessFlags      = description.CpuAccessFlags;
		d3d11_buf_desc.MiscFlags           = description.MiscFlags;

		// buffer options
		d3d11_buf_desc.ByteWidth           = description.ElementSize * description.ElementCount;
		d3d11_buf_desc.StructureByteStride = description.ElementSize;

		GLARE_THROW(d3d11_rs->GetD3DDevice()->CreateBuffer(&d3d11_buf_desc, NULL, &_d3d_buffer), "Error while creating D3D buffer");
	}

	D3D11GpuBuffer::~D3D11GpuBuffer()
	{
		GLARE_SAFE_RELEASE(_d3d_buffer, "Error while releasing D3D buffer");
	}

	GpuResource::Interface* D3D11GpuBuffer::_GetInterface()
	{
		return this;
	}

	GpuResourceFactory* D3D11GpuBuffer::GetFactory() const
	{
		return _factory;
	}

	ID3D11Buffer* D3D11GpuBuffer::GetD3DBuffer() const
	{
		return _d3d_buffer;
	}

	D3D11GpuResourceFactory* D3D11GpuBuffer::GetD3DFactory() const
	{
		return _factory;
	}

	ID3D11Resource* D3D11GpuBuffer::GetD3DResource()
	{
		return _d3d_buffer;
	}
}
